#include "modbusthread.h"

ModbusThread::ModbusThread()
{

}
void ModbusThread::threadStop()
{
    isRun = false;
}

void ModbusThread::run()
{
    isRun = true;
    while (isRun)
    {
        for (auto it = cnf.begin(); it != cnf.end()&&isRun; it++)
        {
            if(std::next(it) == cnf.end()){
                emit readRequest(it.key(), it.value(),true);
            }
            else    emit readRequest(it.key(), it.value(),false);
//            qDebug() << "Address:" << it.key();
//            qDebug() << "Size:" << it.value();
            msleep(200);
        }
    }
    emit threadFinished();
    quit();

}

void ModbusThread::on_Modbus_Config(QMap<QString, int> map)
{
    for (auto it = map.begin(); it != map.end(); ++it) {

        bool ok;
        int key = it.key().toInt(&ok,16); //  QString 的 "1" → 1
        if (ok) {
            cnf[key] = it.value();
        }
    }
}
